package com.example.managemyself.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.os.Build;

import androidx.appcompat.app.AppCompatDelegate;

/**
 * 主题管理器
 * 负责管理应用的主题切换功能
 */
public class ThemeManager {
    
    private static final String PREFS_NAME = "theme_settings";
    private static final String KEY_THEME_MODE = "theme_mode";
    private static final String KEY_CUSTOM_COLORS = "custom_colors";
    private static final String KEY_ACCENT_COLOR = "accent_color";
    
    // 主题模式常量
    public static final int THEME_LIGHT = 0;
    public static final int THEME_DARK = 1;
    public static final int THEME_SYSTEM = 2;
    
    // 强调色选项
    public static final int ACCENT_BLUE = 0;
    public static final int ACCENT_GREEN = 1;
    public static final int ACCENT_PURPLE = 2;
    public static final int ACCENT_ORANGE = 3;
    public static final int ACCENT_RED = 4;
    
    private static ThemeManager instance;
    private final SharedPreferences prefs;
    private final Context context;
    
    private ThemeManager(Context context) {
        this.context = context.getApplicationContext();
        this.prefs = this.context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE);
    }
    
    public static synchronized ThemeManager getInstance(Context context) {
        if (instance == null) {
            instance = new ThemeManager(context);
        }
        return instance;
    }
    
    /**
     * 应用主题设置
     */
    public void applyTheme() {
        int themeMode = getThemeMode();
        
        switch (themeMode) {
            case THEME_LIGHT:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
                break;
            case THEME_DARK:
                AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
                break;
            case THEME_SYSTEM:
            default:
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
                } else {
                    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY);
                }
                break;
        }
    }
    
    /**
     * 设置主题模式
     */
    public void setThemeMode(int themeMode) {
        prefs.edit().putInt(KEY_THEME_MODE, themeMode).apply();
        applyTheme();
    }
    
    /**
     * 获取当前主题模式
     */
    public int getThemeMode() {
        return prefs.getInt(KEY_THEME_MODE, THEME_SYSTEM);
    }
    
    /**
     * 获取主题模式名称
     */
    public String getThemeModeName() {
        int mode = getThemeMode();
        switch (mode) {
            case THEME_LIGHT:
                return "浅色主题";
            case THEME_DARK:
                return "深色主题";
            case THEME_SYSTEM:
            default:
                return "跟随系统";
        }
    }
    
    /**
     * 设置强调色
     */
    public void setAccentColor(int accentColor) {
        prefs.edit().putInt(KEY_ACCENT_COLOR, accentColor).apply();
    }
    
    /**
     * 获取强调色
     */
    public int getAccentColor() {
        return prefs.getInt(KEY_ACCENT_COLOR, ACCENT_BLUE);
    }
    
    /**
     * 获取强调色名称
     */
    public String getAccentColorName() {
        int color = getAccentColor();
        switch (color) {
            case ACCENT_BLUE:
                return "蓝色";
            case ACCENT_GREEN:
                return "绿色";
            case ACCENT_PURPLE:
                return "紫色";
            case ACCENT_ORANGE:
                return "橙色";
            case ACCENT_RED:
                return "红色";
            default:
                return "蓝色";
        }
    }
    
    /**
     * 检查当前是否为深色主题
     */
    public boolean isDarkTheme() {
        int themeMode = getThemeMode();
        if (themeMode == THEME_DARK) {
            return true;
        } else if (themeMode == THEME_LIGHT) {
            return false;
        } else {
            // 跟随系统
            int nightModeFlags = context.getResources().getConfiguration().uiMode & Configuration.UI_MODE_NIGHT_MASK;
            return nightModeFlags == Configuration.UI_MODE_NIGHT_YES;
        }
    }
    
    /**
     * 切换到下一个主题
     */
    public void switchToNextTheme() {
        int currentMode = getThemeMode();
        int nextMode = (currentMode + 1) % 3;
        setThemeMode(nextMode);
    }
    
    /**
     * 重置主题设置
     */
    public void resetThemeSettings() {
        prefs.edit()
                .putInt(KEY_THEME_MODE, THEME_SYSTEM)
                .putInt(KEY_ACCENT_COLOR, ACCENT_BLUE)
                .apply();
        applyTheme();
    }
    
    /**
     * 获取主题统计信息
     */
    public ThemeStatistics getThemeStatistics() {
        return new ThemeStatistics(
                getThemeMode(),
                getAccentColor(),
                isDarkTheme()
        );
    }
    
    /**
     * 主题统计信息类
     */
    public static class ThemeStatistics {
        private final int themeMode;
        private final int accentColor;
        private final boolean isDarkTheme;
        
        public ThemeStatistics(int themeMode, int accentColor, boolean isDarkTheme) {
            this.themeMode = themeMode;
            this.accentColor = accentColor;
            this.isDarkTheme = isDarkTheme;
        }
        
        public int getThemeMode() { return themeMode; }
        public int getAccentColor() { return accentColor; }
        public boolean isDarkTheme() { return isDarkTheme; }
    }
    
    /**
     * 主题变化监听器接口
     */
    public interface ThemeChangeListener {
        void onThemeChanged(int newThemeMode);
        void onAccentColorChanged(int newAccentColor);
    }
} 